package com.imc.platform.demo.controller;

import com.imc.platform.base.user.dao.mapper.IUserMapper;
import com.imc.platform.base.user.pojo.User;
import com.twp.common.tuple.ResultDTO;
import lombok.val;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by tanweiping on 2018/7/20.
 */
@RequestMapping("security")
@Controller
public class SecurityController {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    IUserMapper iUserMapper;

    @Autowired
    PasswordEncoder passwordEncoder;

    /*
    返回页面
     */
    @GetMapping("/loginPage")
    public String login() {
        return "login";
    }

    @RequestMapping(value = "/loginTest")
    @ResponseBody
    //用户名密码是用base64 加密 原文为 admin:admin 即 用户名:密码  内容是放在request.getHeader 的 "authorization" 中
    public ResultDTO login(Principal principal, @RequestParam(name = "logout", required = false) String logout) {
        val ss = iUserMapper.queryAll();
        val sd = iUserMapper.queryUserByLoginName("test");
        if (logout != null) {
            return ResultDTO
                    .builder()
                    .message("注销成功")
                    .build();
        }
        if (principal != null) {
            return ResultDTO
                    .builder()
                    .message("ceshi成功")
                    .build();
        }
        return ResultDTO.builder().isSuccess(false).message("操作失败").build();
    }

    /*
     * SZY  2018/11/29 15:12
     * 方法描述：修改密码
     */
    @RequestMapping(value = "/changePassWord")
    @ResponseBody
    public Map <String, String> changePassWord(HttpServletRequest request) {
        Map <String, String> map = new HashMap <>();
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        String oldPassWord = request.getParameter("oldPassWord");
        //state 1 验证密码是否输入正确  2 修改密码
        String state = request.getParameter("state");
        User userBean = iUserMapper.queryUserById(user.getRoleId());
        boolean flag = passwordEncoder.matches(oldPassWord, userBean.getLoginPwd());
        if (!flag) {
            map.put("state", "1");
            map.put("message", "输入的密码错误");
            return map;
        }
        if ("2".equals(state)) {
            String newPassWord = passwordEncoder.encode(request.getParameter("newPassWord"));
            Map <String, String> map2 = new HashMap <>();
            map2.put("id", user.getRoleId());
            map2.put("pasd", newPassWord);
            try {
                iUserMapper.changePassWord(map2);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }
}
